package com.wanmait.cookbook.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import com.wanmait.cookbook.dao.CookbookCommentDAO;
import com.wanmait.cookbook.dao.LikeDAO;
import com.wanmait.cookbook.dao.WorksCollectDAO;
import com.wanmait.cookbook.dao.WorksCommentDAO;
import com.wanmait.cookbook.dao.WorksDAO;
import com.wanmait.cookbook.dao.WorksImagesDAO;
import com.wanmait.cookbook.dao.WorksReportDAO;
import com.wanmait.cookbook.vo.Cookbook;
import com.wanmait.cookbook.vo.CookbookComment;
import com.wanmait.cookbook.vo.Pager;
import com.wanmait.cookbook.vo.Userinfo;
import com.wanmait.cookbook.vo.Works;
import com.wanmait.cookbook.vo.WorksComment;
import com.wanmait.cookbook.vo.WorksImages;

/**
 * Servlet implementation class HotWorksServlet
 */
@WebServlet("/HotWorksServlet")
@MultipartConfig
public class HotWorksServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HotWorksServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action=request.getParameter("action");
		if("allWorksInc".equals(action)) {
			this.allWorksInc(request, response);
		}else if("allOnePagerWorksInc".equals(action)) {
			this.allOnePagerWorksInc(request, response);
		}else if("upWorks".equals(action)) {
		      this.upWorks(request, response);
	    }else if("showWorks".equals(action)) {
	    	this.showWorks(request, response);
	    }
	}
	 /**
	   *  灞曠ず浣滃搧
	   * @param request
	   * @param response
	   * @throws ServletException
	   * @throws IOException
	   */
	  protected void showWorks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String worksId = request.getParameter("worksId");
	    //WorksImages worksImages = WorksImagesDAO.getWorksImagesDAO().findWorksImagesByWorksId(Integer.parseInt(worksId));
	    //request.setAttribute("worksImages", worksImages);
	    Works showWorks = WorksDAO.getWorksDAO().showWorks(Integer.parseInt(worksId));
	    request.setAttribute("showWorks", showWorks);
	    
	    ArrayList<WorksComment> firstComments = WorksCommentDAO.getWorksCommentDAO().getFirstComments(Integer.parseInt(worksId));
	    request.setAttribute("firstComments", firstComments);
	    
	    
	    Works works = new Works();
	    works.setId(Integer.parseInt(worksId));
	    int likeCount = LikeDAO.getLikeDAO().getLikeCountByWorks(works);
	    request.setAttribute("likeCount", likeCount);
	    

	    HttpSession session = request.getSession();
	    Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
	    if(userinfo!=null) {
		    Boolean flag =	WorksReportDAO.getWorksReportDAO().findWorksReportByUserinfoIdAndWorksId(Integer.parseInt(worksId), userinfo.getId());
		    request.setAttribute("flag", flag);
		    }

	    request.getRequestDispatcher("/showWorks.jsp").forward(request, response);
	  }
	  
	  // 涓婁紶浣滃搧
	  protected void upWorks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    Part part = request.getPart("foodPIC");
	    String fileName = part.getSubmittedFileName();
	    int i = fileName.lastIndexOf(".");
	    String fileext = fileName.substring(i);
	    String filename = UUID.randomUUID().toString();
	    HttpSession session = request.getSession();
	    
	    Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
	
	    
	    String cookbookId = request.getParameter("cookbookId");
	    String info = request.getParameter("content");
	    String title = request.getParameter("title");
	    
	    Cookbook cookbook = new Cookbook();
	    cookbook.setId(Integer.parseInt(cookbookId));
	    Works works = new Works();
	    works.setUserinfo(userinfo);
	    works.setTitle(title);
	    works.setInfo(info);
	    works.setCookbook(cookbook);
	    works.setPic(filename+fileext);
	    WorksDAO.getWorksDAO().upWorks(works);
	    
	    //WorksImagesDAO.getWorksImagesDAO().upWorksImage(filename+fileext);
	    
	    part.write(filename+fileext);
	    part.delete();
	    response.sendRedirect((String)session.getAttribute("referer"));
	  }

	//ajax鑾峰緱鎵�鏈夌殑浣滃搧
	protected void allWorksInc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String pageNo=request.getParameter("pageNo");
		Pager pager=new Pager();
		pager.setPageSize(3);
		int allWorksRows=WorksDAO.getWorksDAO().allWorksRows();
		pager.setPageCount(allWorksRows);
		if(Integer.parseInt(pageNo)<1) {
			pageNo=1+"";
		}
		if(Integer.parseInt(pageNo)>pager.getPageCount()) {
			pageNo=pager.getPageCount()+"";
		}
		pager.setPageNo(Integer.parseInt(pageNo));
		List<Works> hotAllWorks=(List<Works>)WorksDAO.getWorksDAO().pageAllWorks(pager);
		List<Integer> worksCollectCounts=new ArrayList<Integer>();
		for (Works works : hotAllWorks) {
				int count=WorksCollectDAO.getWorksCollectDAO().worksCollectCount(works);
				worksCollectCounts.add(count);
		}
		request.setAttribute("pager", pager);
		request.setAttribute("hotAllWorks",hotAllWorks );
		request.setAttribute("worksCollectCounts",worksCollectCounts );
	
		request.getRequestDispatcher("/inc/ajaxInc/hotWorksInc.jsp").forward(request, response);
	}
	//鑾峰緱绗竴椤典綔鍝�
	protected void allOnePagerWorksInc(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Pager pager=new Pager();
		pager.setPageSize(3);
		int allWorksRows=WorksDAO.getWorksDAO().allWorksRows();
		pager.setPageCount(allWorksRows);
		pager.setPageNo(1);
		List<Works> hotAllWorks=(List<Works>)WorksDAO.getWorksDAO().pageAllWorks(pager);
		List<Integer> worksCollectCounts=new ArrayList<Integer>();
		for (Works works : hotAllWorks) {
				int count=WorksCollectDAO.getWorksCollectDAO().worksCollectCount(works);
				worksCollectCounts.add(count);
		}
		request.setAttribute("hotAllWorks",hotAllWorks );
		request.setAttribute("worksCollectCounts",worksCollectCounts );
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/worksShow.jsp").forward(request, response);
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
